package com.waterms.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.waterms.dao.FileDao;
import com.waterms.model.File;
import com.waterms.util.Pagination;

public class FileServiceImpl implements FileService{
	private FileDao fileDao;

	@Override
	public void add(File file) {
		fileDao.add(file);
	}

	@Override
	public void delete(File file) {
		fileDao.delete(file);
	}

	@Override
	public void update(File file) {
		fileDao.update(file);
	}

	@Override
	public List<File> list() {
		return fileDao.list();
	}

	@Override
	public List<File> list(Pagination page) {
		return list(fileDao.list(), page);
	}

	@Override
	public List<File> list(List<File> files, Pagination page) {
		if (page == null)
		{
			page = new Pagination();
		}
		List<File> result = new ArrayList<File>();
		page.setNum_of_items(files.size());
		int startIndex = page.getCurrent_page() * page.getItems_per_page();
		int endIndex = Math.min(files.size(), startIndex + page.getItems_per_page());
		Collections.sort(files);
		for (int i = startIndex; i < endIndex; ++ i)
		{
			result.add(files.get(i));
		}
		return result;
	}

	@Override
	public List<File> list(String fileName) {
		return fileDao.list(fileName);
	}
	
	
	public FileDao getFileDao() {
		return fileDao;
	}

	public void setFileDao(FileDao fileDao) {
		this.fileDao = fileDao;
	}

}
